// pages/mine/index/index.js
const api = require('../../../api/api.js');
const util = require('../../../utils/util.js');
var app = getApp();
Page({

  /**
   * 页面的初始数据
   */
  data: {
    userInfo: {},
    count: [],
    Domain: api.SCENE_HOST,
    isLogin: wx.getStorageSync("isLogin"),
    login: "登录"
  },

  bindGetUserInfo: function(e) {
    if (!e.detail.userInfo) {
      wx.showToast({
        title: '未授权',
        image: "/images/icon/error_icon.png"
      });
      return;
    }
    var that = this;
    wx.login({
      success: function (res) {
        // 获取code
        var code = res.code;
        util.request(api.OpenidGet + '.' + code, {}).then(function (res) {
          // 获取openid + session_key
          var sessionKey = res.session_key;
          wx.getUserInfo({
            success: function (res) {
              // 获取加密数据
              var iv = res.iv;
              var encryptedData = res.encryptedData;
              // 下面开始调用注册接口
              var data = { code: 'admini', iv: iv, data: encryptedData, sessionKey: sessionKey };
              util.request(api.WeixinOpen, data, 'POST').then(function (res) {
                console.log(res);
              })
            }
          })
        })
      }
    })
  },

  toMineuserTap() {
    if (!this.data.isLogin) {
      wx.navigateTo({
        url: '../../login/login/login'
      })
    } else {
      wx.navigateTo({
        url: '/pages/mine/mineuser/mineuser'
      })
    }
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    let that = this;
    wx.request({
      url: api.MineGetUserProfile,
      data: {

      },
      header: {
        'Content-Type': 'application/json',
        'X-bocang-Authorization': wx.getStorageSync('token')
      },
      method: "POST",
      success: function(res) {
        if (res.data.error_code == 0) {
          that.setData({
            userInfo: res.data.user,
            count: res.data.count,
            isLogin: wx.getStorageSync("isLogin"),
            login: "退出登录"
          });
        } else {
          that.setData({
            userInfo: app.globalData.userInfo,
            isLogin: false,
            login: "登录"
          });
        }
      }
    });
  },

  toOrder0Tap: function() {
    if (this.data.isLogin) {
      wx.navigateTo({
        url: '/pages/order/order/order?status=' + 0,
      })
    } else {
      wx.navigateTo({
        url: '/pages/login/login/login',
      })
    }
  },

  toOrder1Tap: function () {
    if (this.data.isLogin) {
      wx.navigateTo({
        url: '/pages/order/order/order?status=' + 1,
      })
    } else {
      wx.navigateTo({
        url: '/pages/login/login/login',
      })
    }
  },

  toOrder2Tap: function () {
    if (this.data.isLogin) {
      wx.navigateTo({
        url: '/pages/order/order/order?status=' + 2,
      })
    } else {
      wx.navigateTo({
        url: '/pages/login/login/login',
      })
    }
  },

  toOrder5Tap: function () {
    if (this.data.isLogin) {
      wx.navigateTo({
        url: '/pages/order/order/order?status=' + 5,
      })
    } else {
      wx.navigateTo({
        url: '/pages/login/login/login',
      })
    }
  },

  toCollectionTap: function() {
    wx.navigateTo({
      url: '/pages/mine/collection/collection',
    })
  },

  toMessagesTap: function() {
    wx.navigateTo({
      url: '/pages/message/message/message',
    })
  },

  toCartTap: function() {
    wx.switchTab({
      url: '/pages/cart/index/index',
    })
  },

  //地址管理
  toAddressManagement: function() {
    if (this.data.isLogin) {
      wx.navigateTo({
        url: '/pages/address/select-address/select-address',
      })
    } else {
      wx.navigateTo({
        url: '/pages/login/login/login',
      })
    }
  },

  //退出登录
  exitLogin: function() {
    let that = this;
    var isLogin = that.data.isLogin;
    if (isLogin) {
      wx.showModal({
        title: '',
        content: '是否退出登录？',
        success: function (res) {
          if (res.confirm) {
            wx.removeStorageSync('token');
            wx.removeStorageSync('userInfo');
            wx.setStorage({
              key: 'isLogin',
              data: false,
            });
            app.globalData.userInfo = {
              nickname: "昵称",
              level: "等级",
            };
            app.globalData.token = "";
            app.globalData.isLogin = false;
            wx.switchTab({
              url: '/pages/home/index/index',
            });
          }
        }
      })
    } else {
      wx.navigateTo({
        url: '/pages/login/login/login',
      })
    }
    
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})